//
//  MyPrizeViewController.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/2/9.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "MyPrizeViewController.h"
#import "AppHeader.h"
#import "PrizeTableVC.h"

@interface MyPrizeViewController ()

@property (nonatomic, strong) PlatformTopView *topView;

@property (weak, nonatomic) IBOutlet UIScrollView *bgScrollView;
@property (weak, nonatomic) IBOutlet UIView *bottomLineView;

@property (strong, nonatomic) NSArray *titleArray;

@end

@implementation MyPrizeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleArray =@[@"全部", @"已中奖", @"待领取", @"已兑换"];
    self.bgScrollView.contentSize =CGSizeMake(KScreenWidth*_titleArray.count, 0);
    self.bgScrollView.contentOffset =CGPointMake(KScreenWidth*_selectIndex, 0);
    
    [self initPlatformTopView];
    
    [self setUpChildViewController];
    // 2.添加默认控制器
    UIViewController *showVC = self.childViewControllers[_selectIndex];
    showVC.view.frame = self.bgScrollView.bounds;
    [self.bgScrollView addSubview:showVC.view];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    UIViewController *showVC = self.childViewControllers[_selectIndex];
//    [((PrizeTableVC *)showVC) reloadTableView];

}

- (void)setUpChildViewController {
    NSDictionary *parmetd =@{};
    for (NSInteger index =0; index<self.titleArray.count; index++) {
        NSString *title =self.titleArray[index];
        
        if ([title isEqualToString:self.titleArray[0]]) {
            parmetd =@{};
        } else if ([title isEqualToString:self.titleArray[1]]) {
            parmetd =@{};
        } else if ([title isEqualToString:self.titleArray[2]]) {
            parmetd =@{};
        } else if ([title isEqualToString:self.titleArray[3]]) {
            parmetd =@{};
        }
        PrizeTableVC *prizeVC =[[PrizeTableVC alloc]init];
        prizeVC.paramet =parmetd;
        prizeVC.indexRow =index;
        prizeVC.title =title;
        [self addChildViewController:prizeVC];
    }
}

- (void)initPlatformTopView {
    self.topView = [[PlatformTopView alloc]initWithFrame:CGRectMake(15, 0, KScreenWidth-30, 50)];
    self.topView.defaultFontColor =[UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.00];
    self.topView.selectFontColor =APP_DEFAULT_COLOUR;
    self.topView.lineColor =[UIColor clearColor];
    self.topView.selectIndex = _selectIndex;
    self.topView.backgroundColor =[UIColor whiteColor];
    self.topView.cornerRadius =10;
    [self.topView showString:nil withDataArray:self.titleArray.mutableCopy];
    [self.topView didButtonClickBlock:^(UIButton *button, NSInteger index) {
        [self adjustScrollViewContentAndButtonType:index];
    }];
    [self.view addSubview:self.topView];
}
    
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self adjustScrollViewContentAndButtonType:scrollView.contentOffset.x/KScreenWidth];
}
//布局scrollView 改变button状态
- (void)adjustScrollViewContentAndButtonType:(NSInteger)index {
    UIViewController *supVC = [self.childViewControllers objectAtIndex:index];
    [((PrizeTableVC *)supVC) reloadTableView];
    if (!supVC.view.superview) {
        supVC.view.frame = CGRectMake(index*KScreenWidth, 0, KScreenWidth, self.bgScrollView.frame.size.height);
        [self.bgScrollView addSubview:supVC.view];
    }
    _topView.selectIndex =index;
    self.selectIndex =index;
    if (self.selectIndex ==2) {
        self.bottomLineView.hidden =NO;
    } else {
        self.bottomLineView.hidden =YES;
    }
    [self.bgScrollView setContentOffset:CGPointMake(KScreenWidth*index, 0) animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
